/**  
* @Title: ElinkPrinter.java
* @Package com.lemon.utils.printer
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年11月6日
* @version V1.0  
*/
package com.lemon.utils.printer;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import com.lemon.utils.HttpUtil;
import com.lemon.utils.MD5;
/**
 * <p>Title: ElinkPrinter</p>
 * <p>Description: 
 * </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年11月6日
 * @版本 V 1.0 
 */
public class ElinkPrinter {
	private static String partner = "41774";//用户id
	private static String apiKey = "881e41b5ed6f988810918a8463e9f69f690a3685";//API密钥
	private static String mobilephone = "";//注册时绑定的手机号码 
	private static String username = "rc111x";//注册账号

	private static String machine_code = "4004583778";//打印机终端号
	private static String mKey = "3akks4amqqyt";//打印机密钥

	public static void main(String[] args) {
		pmRequest();//查询打印机状态

		StringBuffer sb = new StringBuffer("");
		/*
		 * sb.append("点菜清单\r");
		 * sb.append("----------------------\r");
		 * sb.append("联系人：测试打印\r");
		 * sb.append("电话：13408086368\r");
		 * sb.append("用餐时间：2015-04-09 13:01-13:30\r");
		 * sb.append("用餐地址：打印测试\r");
		 * sb.append("----------------------\r");
		 * sb.append("菜品明细\r\r");
		 * sb.append("1.麻辣牛肉(1份)\r");
		 * sb.append("7.干碟(1份)\r");
		 * sb.append("8.油碟(葱蒜香菜盐味精耗油醋、碗筷)(1份)\r");
		 * sb.append("----------------------\r");
		 * sb.append("器具明细\r\r");
		 * sb.append("电磁炉：1\r");
		 * sb.append("锅具、汤勺、漏勺：1\r");
		 * sb.append("插线板：0\r");
		 * sb.append("----------------------\r");
		 * sb.append("客户备注\r\r");
		 * sb.append("无备注\r");
		 * sb.append("----------------------\r");
		 * sb.append("器具押金：170元\r");
		 * sb.append("外送费用：20.00元\r");
		 * sb.append("菜品金额：272元\r");
		 * sb.append("应付金额：462.0元\r");
		 */

		sb.append("<FS2><center>测试FSC2</center></FS2>");
		sb.append("<center>测试C</center>");
		sb.append("<FB>测试FB</FB>\r\n");
		sb.append("<FS2><FB>测试FS2Fb</FB></FS2>");
		//		System.out.println(sb.toString());

		try {
			//			sendContent(sb.toString());//打印消息
			//sendRequest(sb.toString());//打印消息
			print(sb.toString() , machine_code , mKey);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//打印机是否在线接口0是离线1是在线2是缺纸
	public static boolean pmRequest() {
		Map<String , String> params = new HashMap<String , String>();
		params.put("machine_code" , machine_code);
		params.put("partner" , partner);
		String sign = signRequest(params , mKey);
		params.put("sign" , sign);
		HttpClient httpClient = new HttpClient();
		PostMethod post = new PostMethod("http://open.10ss.net:8888/getstatus.php");

		for (Map.Entry<String , String> entry : params.entrySet()) {
			post.addParameter(entry.getKey() , entry.getValue());
		}

		HttpMethodParams param = post.getParams();
		param.setContentCharset("UTF-8");

		try {
			httpClient.executeMethod(post);
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//如果返回200，表明成功
		if (post.getStatusCode() == 200) {
			try {
				String result;
				result = post.getResponseBodyAsString();
				System.out.println(result);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return true;
		} else {
			System.out.println("打印失败！");
			return false;
		}

	}

	/**
	 * <b>Description:<blockquote>
	 * </blockquote></b>
	 * 
	 * @Title: print
	 * @param content 内容
	 * @param code    = 數據庫key
	 * @param mkey    = 數據庫密鑰
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月14日
	 */
	public static boolean print(String content , String code , String mkey) {
		try {
			Map<String , String> params = new HashMap<String , String>();
			params.put("partner" , partner);
			params.put("machine_code" , machine_code);
			String time = String.valueOf(System.currentTimeMillis());
			params.put("time" , time);
			String sign = signRequest(params , mkey);

			byte[] data = ("partner=" + partner + "&machine_code=" + machine_code + "&content=" + content + "&sign="
					+ sign + "&time=" + time).getBytes();
			URL url = new URL("http://open.10ss.net:8888");
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("POST");
			conn.setConnectTimeout(5 * 1000);
			conn.setDoOutput(true);
			conn.setRequestProperty("Content-Type" , "text/html; charset=utf-8");
			conn.setRequestProperty("Content-Length" , String.valueOf(data.length));

			//获取输出流
			OutputStream outStream = conn.getOutputStream();
			//传入参数
			outStream.write(data);
			outStream.flush();
			outStream.close();

			//获取输入流
			InputStream is = conn.getInputStream();

			System.out.println(conn.getResponseCode());
			if (conn.getResponseCode() == 200) {
				int i = -1;
				byte[] b = new byte[1024];
				StringBuffer result = new StringBuffer();
				while ((i = is.read(b)) != -1) {
					result.append(new String(b, 0, i));
				}

				String sub = result.toString();
				if (sub.equals("1")) {//数据已经发送到客户端
					System.out.println("打印成功");
					return true;
				} else {
					System.out.println("打印失败,返回值：" + result);
					return false;
				}
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}
	//打印机打印消息
	public static boolean sendRequest(String content) {
		Map<String , String> params = new HashMap<String , String>();
		params.put("partner" , partner);
		params.put("machine_code" , machine_code);
		params.put("content" , content);
		String sign = signRequest(params , mKey);
		params.put("sign" , sign);

		HttpClient httpClient = new HttpClient();
		PostMethod post = new PostMethod("http://open.10ss.net:8888");

		for (Map.Entry<String , String> entry : params.entrySet()) {
			post.addParameter(entry.getKey() , entry.getValue());
		}

		HttpMethodParams param = post.getParams();
		param.setContentCharset("UTF-8");

		try {
			httpClient.executeMethod(post);
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//如果返回200，表明成功
		if (post.getStatusCode() == 200) {
			try {
				String result;
				result = post.getResponseBodyAsString();
				if (result.equals("1")) {//数据已经发送到客户端
					System.out.println("打印成功");
					return true;
				} else {
					System.out.println("打印失败,返回值：" + result);
					return false;
				}

			} catch (IOException e) {
				e.printStackTrace();
			}
			return true;
		} else {
			System.out.println("打印失败！");
			return false;
		}

	}

	public static boolean sendContent(String content) {
		try {
			Map<String , String> params = new HashMap<String , String>();
			params.put("partner" , partner);
			params.put("machine_code" , machine_code);
			String time = String.valueOf(System.currentTimeMillis());
			params.put("time" , time);
			String sign = signRequest(params , mKey);

			byte[] data = ("partner=" + partner + "&machine_code=" + machine_code + "&content=" + content + "&sign="
					+ sign + "&time=" + time).getBytes();
			URL url = new URL("http://open.10ss.net:8888");
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("POST");
			conn.setConnectTimeout(5 * 1000);
			conn.setDoOutput(true);
			conn.setRequestProperty("Content-Type" , "text/html; charset=utf-8");
			conn.setRequestProperty("Content-Length" , String.valueOf(data.length));

			//获取输出流
			OutputStream outStream = conn.getOutputStream();
			//传入参数
			outStream.write(data);
			outStream.flush();
			outStream.close();

			//获取输入流
			InputStream is = conn.getInputStream();

			System.out.println(conn.getResponseCode());
			if (conn.getResponseCode() == 200) {
				int i = -1;
				byte[] b = new byte[1024];
				StringBuffer result = new StringBuffer();
				while ((i = is.read(b)) != -1) {
					result.append(new String(b, 0, i));
				}

				String sub = result.toString();
				if (sub.equals("1")) {//数据已经发送到客户端
					System.out.println("打印成功");
					return true;
				} else {
					System.out.println("打印失败,返回值：" + result);
					return false;
				}
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 打印签名
	 * 
	 * @param params
	 * @return
	 */
	public static String signRequest(Map<String , String> params , String mkey) {
		Map<String , String> sortedParams = new TreeMap<String , String>();
		sortedParams.putAll(params);
		Set<Map.Entry<String , String>> paramSet = sortedParams.entrySet();
		StringBuilder query = new StringBuilder();
		query.append(apiKey);
		for (Map.Entry<String , String> param : paramSet) {
			query.append(param.getKey());
			query.append(param.getValue());
		}
		query.append(mkey);

		System.out.println(query.toString());
		String encryptStr = MD5.getMD5(query.toString()).toUpperCase();
		return encryptStr;
	}

	/**
	 * <b>Description:<blockquote>
	 * </blockquote></b>
	 * 
	 * @Title: addClient
	 * @param code       //打印机上面的机械编码
	 * @param mkey       //打印机上面的可以
	 * @param clientName //用户输入的打印机名称
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月14日
	 */
	public static String addClient(String code , String mkey , String clientName) {
		String url_ = "http://open.10ss.net:8888/addprint.php";
		try {
			Map<String , String> params = new HashMap<String , String>();

			params = new HashMap<String , String>();
			params.put("partner" , partner);
			params.put("machine_code" , code);
			params.put("username" , username);
			params.put("printname" , clientName);
			params.put("mobilephone" , mobilephone);
			String sign = signRequest(params , mkey);
			params.put("msign" , mkey);
			params.put("sign" , sign);
			String doPost = HttpUtil.doPost(url_ , params);
			return doPost;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
}
